package com.fuxi.cloud.common.util;

/**
 * CommonUtils
 *
 * @Author: 扫地僧
 * @CreateTime: 2021-04-19
 */
import cn.hutool.core.util.ObjectUtil;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import javax.servlet.http.HttpServletRequest;
import javax.sql.DataSource;

import com.fuxi.cloud.common.expetion.JimuReportException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

public class CommonUtils {
    private static final Logger log = LoggerFactory.getLogger(CommonUtils.class);
    private static String DB_TYPE = "";

    public CommonUtils() {
    }

    public static String getFileName(String fileName) {
        int var1 = fileName.lastIndexOf(47);
        int var2 = fileName.lastIndexOf(92);
        int var3 = var2 > var1 ? var2 : var1;
        if (var3 != -1) {
            fileName = fileName.substring(var3 + 1);
        }

        fileName = fileName.replace("=", "").replace(",", "").replace("&", "").replace("#", "");
        return fileName;
    }

    public static String upload(MultipartFile file, String bizPath, String uploadType) {
        String var3 = "";
        if ("minio".equals(uploadType)) {
            var3 = MinioUtil.upload(file, bizPath);
        } else {
            var3 = OssBootUtil.upload(file, bizPath);
        }

        return var3;
    }

    public static String upload(MultipartFile file, String bizPath, String uploadType, String customBucket) {
        String var4 = "";
        if ("minio".equals(uploadType)) {
            var4 = MinioUtil.upload(file, bizPath, customBucket);
        } else {
            var4 = OssBootUtil.upload(file, bizPath, customBucket);
        }

        return var4;
    }

    public static String getDatabaseType() {
        if (oConvertUtils.isNotEmpty(DB_TYPE)) {
            return DB_TYPE;
        } else {
            DataSource var0 = (DataSource)JimuSpringContextUtils.getApplicationContext().getBean(DataSource.class);

            try {
                return getDatabaseTypeByDataSource(var0);
            } catch (SQLException var2) {
                log.warn(var2.getMessage());
                return "";
            }
        }
    }

    private static String getDatabaseTypeByDataSource(DataSource dataSource) throws SQLException {
        if ("".equals(DB_TYPE)) {
            Connection var1 = dataSource.getConnection();

            try {
                DatabaseMetaData var2 = var1.getMetaData();
                String var3 = var2.getDatabaseProductName().toUpperCase();
                if (var3.indexOf("MYSQL") >= 0) {
                    DB_TYPE = "MYSQL";
                } else if (var3.indexOf("ORACLE") >= 0) {
                    DB_TYPE = "ORACLE";
                } else if (var3.indexOf("SQLSERVER") < 0 && var3.indexOf("SQL SERVER") < 0) {
                    if (var3.indexOf("POSTGRESQL") < 0) {
                        throw new JimuReportException("数据库类型:[" + var3 + "]不识别!");
                    }

                    DB_TYPE = "POSTGRESQL";
                } else {
                    DB_TYPE = "SQLSERVER";
                }
            } catch (Exception var7) {
                log.error(var7.getMessage(), var7);
            } finally {
                var1.close();
            }
        }

        return DB_TYPE;
    }

    public static Integer getIntParameter(String params, Integer def) {
        HttpServletRequest var2 = JimuSpringContextUtils.getHttpServletRequest();
        String var3 = var2.getParameter(params);
        return isNotEmpty(var3) ? Integer.parseInt(var3.toString()) : def;
    }

    public static Integer getIntParameter(HttpServletRequest request, String params, Integer def) {
        String var3 = request.getParameter(params);
        return isNotEmpty(var3) ? Integer.parseInt(var3.toString()) : def;
    }

    public static String filter(String str) throws PatternSyntaxException {
        String var1 = "[`_《》~!@#$%^&*()+=|{}':;',\\[\\].<>?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
        Pattern var2 = Pattern.compile(var1);
        Matcher var3 = var2.matcher(str);
        return var3.replaceAll("").trim();
    }

    public static boolean anyOneIsNull(Object... objs) {
        int var1 = objs.length;

        for(int var2 = 0; var2 < var1; ++var2) {
            Object var3 = objs[var2];
            if (isEmpty(var3)) {
                return true;
            }
        }

        return false;
    }

    public static boolean anyOneIsNotNull(Object... objs) {
        int var1 = objs.length;

        for(int var2 = 0; var2 < var1; ++var2) {
            Object var3 = objs[var2];
            if (isNotEmpty(var3)) {
                return true;
            }
        }

        return false;
    }

    public static boolean allIsNull(Object... objs) {
        int var1 = objs.length;
        int var2 = 0;

        for(int var3 = 0; var3 < var1; ++var3) {
            Object var4 = objs[var3];
            if (isEmpty(var4)) {
                ++var2;
            }
        }

        if (var2 == var1) {
            return true;
        } else {
            return false;
        }
    }

    public static <T> Long[] listToLong(List<T> list) {
        Long[] var1 = (Long[])list.toArray(new Long[list.size()]);
        return var1;
    }

    public static Boolean gt(BigDecimal v1, BigDecimal v2) {
        Boolean var2 = false;
        if (v1.compareTo(v2) == 1) {
            var2 = true;
        }

        return var2;
    }

    public static Boolean ge(BigDecimal v1, BigDecimal v2) {
        Boolean var2 = false;
        if (v1.compareTo(v2) == 1) {
            var2 = true;
        }

        if (v1.compareTo(v2) == 0) {
            var2 = true;
        }

        return var2;
    }

    public static Boolean lt(BigDecimal v1, BigDecimal v2) {
        Boolean var2 = false;
        if (v1.compareTo(v2) == -1) {
            var2 = true;
        }

        return var2;
    }

    public static Boolean eq(BigDecimal v1, BigDecimal v2) {
        Boolean var2 = false;
        if (isNotEmpty(v1) && v1.compareTo(v2) == 0) {
            var2 = true;
        }

        return var2;
    }

    public static Boolean le(BigDecimal v1, BigDecimal v2) {
        Boolean var2 = false;
        if (isNotEmpty(v1)) {
            if (v1.compareTo(v2) == -1) {
                var2 = true;
            }

            if (v1.compareTo(v2) == 0) {
                var2 = true;
            }
        }

        return var2;
    }

    public static Boolean gtzero(BigDecimal v) {
        Boolean var1 = false;
        if (isNotEmpty(v) && v.doubleValue() > 0.0D) {
            var1 = true;
        }

        return var1;
    }

    public static Boolean gezero(BigDecimal v) {
        Boolean var1 = false;
        if (isNotEmpty(v) && (v.compareTo(BigDecimal.ZERO) == 0 || v.compareTo(BigDecimal.ZERO) == 1)) {
            var1 = true;
        }

        return var1;
    }

    public static Boolean eqzero(BigDecimal v) {
        Boolean var1 = false;
        if (isNotEmpty(v) && v.compareTo(BigDecimal.ZERO) == 0) {
            var1 = true;
        }

        return var1;
    }

    public static Boolean nezero(BigDecimal v) {
        Boolean var1 = true;
        if (isNotEmpty(v) && v.compareTo(BigDecimal.ZERO) == 0) {
            var1 = false;
        }

        return var1;
    }

    public static Boolean ltzero(BigDecimal v) {
        Boolean var1 = false;
        if (isNotEmpty(v) && v.doubleValue() < 0.0D) {
            var1 = true;
        }

        return var1;
    }

    public static Boolean lezero(BigDecimal v) {
        Boolean var1 = false;
        if (isNotEmpty(v)) {
            if (v.compareTo(BigDecimal.ZERO) == 0) {
                var1 = true;
            }

            if (v.compareTo(BigDecimal.ZERO) == -1) {
                var1 = true;
            }
        }

        return var1;
    }

    public static boolean contains(String[] stringArray, String source) {
        List var2 = Arrays.asList(stringArray);
        return var2.contains(source);
    }

    public static boolean contains(List<String> stringArray, String source) {
        return stringArray.contains(source);
    }

    public static boolean allNotNull(Object... obj) {
        return !anyOneIsNull(obj);
    }

    public static boolean isNotEmpty(Object obj) {
        return !isEmpty(obj);
    }

    public static boolean isEmpty(Object obj) {
        if (obj == null) {
            return true;
        } else if (obj.getClass().isArray()) {
            return Array.getLength(obj) == 0;
        } else if (obj instanceof CharSequence) {
            return ((CharSequence)obj).length() == 0;
        } else if (obj instanceof String) {
            return ((String)obj).trim().equals("");
        } else if (obj instanceof Collection) {
            return ((Collection)obj).isEmpty();
        } else {
            return obj instanceof Map ? ((Map)obj).isEmpty() : false;
        }
    }

    public static String renamePic(String fileName) {
        String var1 = fileName.substring(fileName.lastIndexOf("."));
        return UUID.randomUUID().toString().replace("-", "") + var1;
    }

    public static BigDecimal getBigDecimal(BigDecimal v, String def) {
        return isEmpty(v) ? new BigDecimal(def) : v;
    }

    public static BigDecimal getBigDecimal(BigDecimal v, BigDecimal def) {
        return isEmpty(v) ? def : v;
    }

    public static String getString(String v) {
        return isEmpty(v) ? "" : v;
    }

    public static String getString(String v, String defval) {
        return isEmpty(v) ? defval : v;
    }

    public static Integer getInteger(Integer v, Integer defval) {
        return isEmpty(v) ? defval : v;
    }

    public static Integer getInteger(String v, Integer defval) {
        return isEmpty(v) ? defval : Integer.parseInt(v);
    }

    public static String chineseNumber2Int(String chineseNumber) {
        String[] var1 = new String[]{"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
        String[] var2 = new String[]{"十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千"};
        String var3 = "";
        int var4 = chineseNumber.length();

        for(int var5 = 0; var5 < var4; ++var5) {
            int var6 = chineseNumber.charAt(var5) - 48;
            if (var5 != var4 - 1 && var6 != 0) {
                var3 = var3 + var1[var6] + var2[var4 - 2 - var5];
            } else {
                var3 = var3 + var1[var6];
            }

            System.out.println("  " + var3);
        }

        return var3;
    }

    public static String getAlias(String tableName) {
        String[] var1 = tableName.split("_");
        String var2 = "";
        if (var1.length > 2) {
            String[] var3 = var1;
            int var4 = var1.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                String var6 = var3[var5];
                var2 = var2 + var6.substring(0, 1);
            }
        } else {
            var2 = var1[var1.length - 1];
        }

        return var2;
    }

    public static String string2Unicode(String string) {
        String var1 = "";
        if (isNotEmpty(string)) {
            StringBuffer var2 = new StringBuffer();

            for(int var3 = 0; var3 < string.length(); ++var3) {
                char var4 = string.charAt(var3);
                var2.append("\\u" + Integer.toHexString(var4));
            }

            var1 = var2.toString();
        }

        return var1;
    }

    public static Long[] StringArray2LongArray(String[] stringArray) {
        ArrayList var1 = new ArrayList();
        String[] var2 = stringArray;
        int var3 = stringArray.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            String var5 = var2[var4];

            try {
                var1.add(Long.parseLong(var5));
            } catch (NumberFormatException var7) {
                var7.printStackTrace();
            }
        }

        Long[] var8 = (Long[])var1.toArray(new Long[var1.size()]);
        return var8;
    }

    public static String getString(Object obj, String def) {
        return ObjectUtil.isNotEmpty(obj) ? obj.toString() : def;
    }

    public static Long getLong(Object obj, Long def) {
        return ObjectUtil.isNotEmpty(obj) ? Long.parseLong(obj.toString()) : def;
    }
}
